# -*- coding: UTF-8 -*-
import datetime as dt
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from smtplib import SMTPException


class Timer:

    def __init__(self):
        self.start_dt = None

    def start(self):
        self.start_dt = dt.datetime.now()

    def stop(self):
        end_dt = dt.datetime.now()
        print('Time taken: %s' % (end_dt - self.start_dt))


class MailSender:

    def __init__(self):
        self.receivers = ['594931051@qq.com', '418796187@qq.com', '827814990@qq.com']

    def send_email(self, mail_content, mail_title):
        sender_qq = '594931051'
        sender_qq_mail = '594931051@qq.com'
        pwd = 'nweukmdovbzkbdhg'
        host_server = 'smtp.qq.com'
        try:
            smtp = SMTP_SSL(host_server)
            # 参数值为1表示开启调试模式，参数值为0关闭调试模式
            smtp.set_debuglevel(0)
            smtp.ehlo(host_server)
            smtp.login(sender_qq, pwd)
            msg = MIMEText(mail_content, "plain", 'utf-8')
            msg["Subject"] = Header(mail_title, 'utf-8')
            msg["From"] = sender_qq_mail
            smtp.sendmail(sender_qq_mail, self.receivers, msg.as_string())
            print("Successfully sent email")
            smtp.quit()
        except SMTPException:
            print("Error: unable to send email")


if __name__ == '__main__':
    MailSender().send_email(mail_content='信号出错测试邮件-正文内容', mail_title='信号出错')
